# coding: utf-8
# @Author: ChuiZi
import logging
import pytest
import allure

# <editor-fold desc="只是在模板中用到引用库">
from lib.frameTool.achieveTestDatas import LoadTestDatas
# </editor-fold>

from settings import *
from settings import caseDescript
from lib.common.requests_pack import RequestsCommon
from lib.frameTool.decide import decide

scenes = LoadTestDatas().run()[0]
caseTap = {allureTapEpic: f'{scenes[0][0]}', allureTapFeature: f'{scenes[0][1]}', allureTapStory: f'{scenes[0][2]}'}
scenes = scenes[1]


@allure.epic(caseTap.get(allureTapEpic))
@allure.feature(caseTap.get(allureTapFeature))
class Test_cls:
    @allure.story(caseTap.get(allureTapStory))
    @pytest.mark.parametrize('title', scenes.keys(), ids=list(scenes.keys()))
    @pytest.mark.order(None)  # 用例执行顺序
    def test_func(self, title, set_global_data, get_global_data, one_session, one_id):
        allure.dynamic.title(title)

        reqData = scenes[title]  # 对应用例的测试数据
        reqDataDescription = reqData.get(caseDescript)
        reqDataHeaders = {}  # 请求头
        reqDataBody = {}  # 请求体
        reqDataHeaders.update(header := reqData.get(reqHeader))
        reqDataBody.update(body := reqData.get(reqBody))
        req = RequestsCommon(meth=reqData.get(reqMeth), url=reqData.get(reqUrl), bodyType=reqData.get(reqBodyType),
                             body=reqDataBody, headers=reqDataHeaders, session=one_session).connect()
        if switch["debug"]:
            # print(reqDataBody)
            print(f"\n测试场景' {title} '的返回值:{req.content.decode('utf-8')[0:100]}")
            # print(reqDataHeaders)
        if testCondition := reqData.get(checkData):  # 存在需要校验的数据,则进入请求及判断逻辑,否则直接通过
            decide_data = decide(req, testCondition, reqDataDescription)
            response = req.json()
            assert decide_data[0], decide_data[1]
        else:
            print("无需校验")
            assert True